import React, { useState, useEffect} from 'react';
import { useHistory } from "react-router-dom";  
import http from '../../../model/http';
import code from "../../../model/code";
import './list.less'; 
import {Badge,ActivityIndicator, List, Toast, Modal, TextareaItem} from 'antd-mobile';
import Header from "../../../components/header/Header";
const Item = List.Item;
function DoctorWorkList() { 
  const history = useHistory();
  const [userDetail, setUserDetail] = useState('')
  const [load, setLoad] = useState(false)
  const [jurisdiction, setJurisdiction] = useState('')
  // const [mark, setMark] = useState('') 
  let mark = '';
  /*渲染完成*/ 
  useEffect( async ()=>{
    console.log(history.location.state.operationInfo)
    setUserDetail(history.location.state.operationInfo)

    // 获取session 判断是否医生
    setJurisdiction(sessionStorage.getItem('doctorJurisdiction')?JSON.parse(sessionStorage.getItem('doctorJurisdiction')):'')
    
  },[])
  

  // 点击开始按钮
  const beginClick = async () => {
    console.log('触发') 
    // optype = 1 开始
    // optype = 2 结束
    
    Modal.alert('提示', '确认开始治疗', [
      { text: '取消' },
      {
        text: '确认', onPress: async () => {
          opCureFunc(1)
          setLoad(true)
        }
      }
    ])
  }
  const setMarkChange = (v) => {  
    // setMark(v)
    mark = v
  }
  const modelHtml = ( 
    <List className='g-p5-10 g-bg-f g-bdrs12' style={{border:'1px solid #ddd'}}>
      <TextareaItem defaultValue={mark} rows={5} count={50} style={{fontSize:'14px'}} className="input-css" onChange={(v) => setMarkChange(v)} placeholder='请输入跳诊原因...' /> 
    </List> 
  )
  // 跳诊
  const removeClick = () =>{
    Modal.alert(<span style={{fontSize:'15px'}}>确认跳过此次治疗</span>, modelHtml, [
      { text: '取消' },
      {
        text: '确认', onPress: async () => {
          if (mark == '') { Toast.fail('请先输入跳诊原因'); return };
          opCureFunc(3)
          setLoad(true)
        }
      }
    ])
  }
  // 点击结束按钮
  const endClick = async () => {
    console.log('触发结束') 
    Modal.alert('提示', '确认治疗结束', [
      { text: '取消' },
      {
        text: '确认', onPress: async () => {
          opCureFunc(2) 
          setLoad(true)
        }
      }
    ])
  }

  const opCureFunc = async (type) => {
    const result = await http.post('/expert/opCure',{
      optype: type,
      schedulerReportId: userDetail.srid,
      jumpreason: type == 3 ? mark : undefined
    })
    if(result.code == 0){ 
      setLoad(false)
      Toast.success(result.msg)
      setTimeout(() => {
        history.push(`/doctorWorkstation`) 
      }, 500);
    }else{
      setLoad(false)
      Toast.fail(result.msg)
    }
  }

 
  
   
  return ( 
    <div className='v-doctor-work-station-list'>
        <List className="my-list"> 
          <Item extra={userDetail.customer_id}>顾客卡号</Item>
          <Item extra={userDetail.customer_name}>顾客名称</Item>
          <Item extra={userDetail.trnames}>治疗项目</Item>
          <Item extra={userDetail.hocusTypeName}>麻醉类型</Item>
          <Item extra={userDetail.expertname}>治疗医生</Item>
          <Item extra={userDetail.sch_date}>治疗日期</Item>
          <Item extra={userDetail.begin_time}>开始时间</Item>
          <Item extra={userDetail.end_time}>结束时间</Item>
          <Item extra={`${userDetail.estimate_time}分钟`}>治疗时长</Item>
          <Item extra={userDetail.consult_man_name}>所属</Item> 
        </List> 
        {userDetail.confirm_state === 0 && jurisdiction.authority != '1' && jurisdiction.authority != '5' && jurisdiction.authority != '6' && jurisdiction.authority != '8' ? (<div style={{display:'flex'}}><button onClick={() => beginClick()}>开 始</button><button onClick={() => removeClick()} className="btn-end-style">跳 诊</button></div>):''}


        {userDetail.confirm_state === 1 && jurisdiction.authority != '1' && jurisdiction.authority != '5' && jurisdiction.authority != '6' && jurisdiction.authority != '8' ? (<button className="btn-end-style" onClick={() => endClick()}>结 束</button>):''} 

        {userDetail.confirm_state === 2 && jurisdiction.authority != '1' && jurisdiction.authority != '5' && jurisdiction.authority != '6' && jurisdiction.authority != '8' ? (<button className="btn-end-style2" disabled>治疗完成</button>):''}


        {userDetail.confirm_state === 3 && jurisdiction.authority != '1' && jurisdiction.authority != '5' && jurisdiction.authority != '6' && jurisdiction.authority != '8' ? (<button className="btn-end-style2" disabled>跳 诊</button>):''}

        <ActivityIndicator
          toast
          text="Loading..."
          animating={load}
        />
        <Header />
    </div>
  )
}

export default DoctorWorkList;
// 0 未开启
// 1 进行中
// 2 已完成
 